<template>
  <el-dialog
    :title="title"
    :visible.sync="Visible"
  >
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="right"  label-width="100px" class="small-space">
      <el-row v-loading.body="listLoading">
        <el-col :span="15" :offset="2">
          <el-form-item prop="password" label="密码">
            <el-input name="password" :type="pwdShow?'text':'password'" v-model="ruleForm.password" autoComplete="on" placeholder="密码" />
            <span class='show-pwd' @click='pwdShow = !(pwdShow)'><icon-svg icon-class="yanjing" /></span>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { editPassword } from '@/api/staff'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String
    },
    editId: {}
  },
  data() {
    return {
      pwdShow: false,
      listLoading: false,
      Visible: this.dialogVisible,
      ruleForm: {
        password: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入密码' }
        ]
      }
    }
  },
  watch: {
    dialogVisible() {
      console.log('dialogVisible', this.dialogVisible)
      if (this.dialogVisible) {
        this.Visible = this.dialogVisible
      }
    },
    Visible() {
      console.log('Visible', this.Visible)
      this.$emit('closeDialog')
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.listLoading = true
          editPassword(this.editId, this.ruleForm.password).then(() => {
            this.$message({ message: '保存成功', type: 'success' })
            this.resetForm('ruleForm')
            this.Visible = false // 关闭对话框
            this.listLoading = false
          })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped rel="stylesheet/scss" lang="scss">
  $dark_gray:#889aa4;
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
  }
</style>
